﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BigDataSync.Enums;

namespace BigDataSync
{
    class Program
    {
        private static List<string> HelpArguments { get; set; }

        static Program()
        {
            HelpArguments = new List<string>()
            {
                "-h",
                "/h",
                "h",
                "help"
            };
        }

        static void Main(string[] args)
        {
            SayHello();

            if (HelpArguments.Contains(args[0].ToString()))
            {
                ShowHelpText();
                return;
            }

            if (string.Compare(args[0], BigDataSyncActions.syncEvents.ToString(), StringComparison.OrdinalIgnoreCase) == 0)
            {
                if (args.Length > 1)
                {
                    if (HelpArguments.Contains(args[1]))
                        ExplainSyncEvents();
                    else
                    {
                        Console.WriteLine("Unable to parse input arguments");
                        ShowHelpText();
                    }
                }
                else
                {
                    Console.WriteLine("Performing Event Sync");
                    EventSyncWorker.DoWork();
                    Console.WriteLine("Event Sync complete");
                }
            }
            else if (string.Compare(args[0], BigDataSyncActions.updateUserObjects.ToString(), StringComparison.OrdinalIgnoreCase) == 0)
            {
                if (args.Length > 1)
                {
                    if (HelpArguments.Contains(args[1]))
                        ExplainUpdateUserObjects();
                    else
                    {
                        Console.WriteLine("Unable to parse input arguments");
                        ShowHelpText();
                    }
                }
                else
                {
                    Console.WriteLine("Performing User Object Update");
                    UserObjectUpdateWorker.DoWork();
                    Console.WriteLine("User Object Update complete");
                }
            }
            else if (args.Length == 0)
                NoInputArgumentsRead();
            else
            {
                Console.WriteLine("Unable to parse input arguments");
                ShowHelpText();
            }

            EndBatch();
        }

        public static void SayHello()
        {
            Console.WriteLine("*********************");
            Console.WriteLine(string.Format("Big Data Sync Job Start {0}", DateTime.Now.ToString()));
        }

        public static void ShowHelpText()
        {
            Console.WriteLine("*********************");
            Console.WriteLine("");
            Console.WriteLine("         Help        ");
            Console.WriteLine("*********************");
            Console.WriteLine();
            ExplainSyncEvents();
            ExplainUpdateUserObjects();
        }

        public static void ExplainSyncEvents()
        {
            Console.WriteLine("Sync Events Explanation");
            Console.WriteLine(string.Format("Accepted Argument: {0}", BigDataSyncActions.syncEvents.ToString()));
            Console.WriteLine("This will Sync the events collection with the other registered collections which are dirty");
            Console.WriteLine("-----------------------------------------------------------------");
        }

        public static void ExplainUpdateUserObjects()
        {
            Console.WriteLine("Update User Objects Explanation");
            Console.WriteLine(string.Format("Accepted Argument: {0}", BigDataSyncActions.updateUserObjects.ToString()));
            Console.WriteLine("This will attempt to update the registered collections with data pulled from their corresponding API");
            Console.WriteLine("-----------------------------------------------------------------");
        }

        public static void NoInputArgumentsRead()
        {
            Console.WriteLine("No input arguments were read");
        }

        public static void EndBatch()
        {
            Console.WriteLine(string.Format("Batch Complete {0}", DateTime.Now.ToString()));
        }
    }
}
